import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_loginpage/3681/login_success.dart';
import 'package:flutter_loginpage/3681/register.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:toast/toast.dart';

class YXMPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: "LoginPage"),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  bool _passwordVisibleFlag = true;
  bool _checkBoxFlag = false;

  TextEditingController _editingController0=new TextEditingController();//用户名栏的控制器
  TextEditingController _editingController1=new TextEditingController();//密码栏的控制器
  String inputUserName,inputPassWord;
  String truePassWord;

  void _getText(){
    setState(() {
      inputUserName=_editingController0.text;
      inputPassWord=_editingController1.text;
      print("userName: "+inputUserName+"\npassWord: "+inputPassWord);
    });
  }

  @override
  Widget build(BuildContext context) {
    // save() async{
    //   SharedPreferences prefs = await SharedPreferences.getInstance();
    //   prefs.setString(userName, passWord);
    // }
    Future<String> get() async{
      var mPassWord;
      SharedPreferences prefs = await SharedPreferences.getInstance();
      mPassWord = prefs.getString(inputUserName);
      return mPassWord;
    }

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title,style: TextStyle(),),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Align(
              alignment: Alignment.topLeft,
              child: Column(
                children: <Widget>[
                  Align(
                      alignment: Alignment.topLeft,
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(20, 20,0,0),
                        child: Image.asset(
                          "images/baidu_logo.png",
                          width: 120,
                        ),
                      )
                  ),
                  Align(
                      alignment: Alignment.topLeft,
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(20, 0,0,0),
                        child: Text(
                          "欢迎登录百度帐户",
                          textAlign: TextAlign.left,
                          style: TextStyle(
                              fontSize: 30,
                              fontWeight: FontWeight.bold
                          ),
                        ),
                      )
                  ),
                ],
              ),
            ),
            SizedBox(height: 100,),
            Padding(
                padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                child: Container(
                  height: 60,
                  child: TextField(
                    controller: _editingController0,
                    keyboardType: TextInputType.url,
                    maxLines: 1,//最大行数
                    // maxLength: 30,//最大长度
                    decoration: InputDecoration(
                      // labelText: "账户名/手机号/邮箱",
                      // labelStyle: TextStyle(
                      //   color: Colors.grey,
                      //   fontSize: 16
                      // ),
                      // hintText: "输入...",
                        hintText: "请输入手机号/用户名/邮箱",
                        hintStyle: TextStyle(fontSize: 16),
                        fillColor: Colors.black12,//背景颜色，必须配合filled为true才能显示
                        filled: true,//背景颜色，必须配合fillColor才能显示
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                            borderSide: BorderSide.none
                        )
                    ),
                  ),
                )
            ),
            Padding(
                padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
                child: Container(
                  height: 60,
                  child: TextField(
                    controller: _editingController1,
                    keyboardType: TextInputType.url,
                    maxLines: 1,//最大行数
                    // maxLength: 30,//最大长度
                    obscureText: _passwordVisibleFlag,
                    decoration: InputDecoration(
                      // labelText: "密码",
                      // labelStyle: TextStyle(
                      //     color: Colors.grey,
                      //     fontSize: 16
                      // ),
                      // hintText: "输入...",
                        hintText: "请输入登陆密码",
                        hintStyle: TextStyle(fontSize: 16),
                        fillColor: Colors.black12,//背景颜色，必须配合filled为true才能显示
                        filled: true,//背景颜色，必须配合fillColor才能显示
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                            borderSide: BorderSide.none
                        ),
                        suffixIcon: IconButton(
                          icon: Icon(_passwordVisibleFlag ? Icons.visibility : Icons.visibility_off),
                          onPressed: (){
                            setState(() {
                              _passwordVisibleFlag = !_passwordVisibleFlag;
                            });
                          },
                        )
                    ),
                  ),
                )
            ),
            Padding(
                padding: EdgeInsets.fromLTRB(20, 10, 20, 0),
                child: Container(
                  // alignment: Alignment.centerRight,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      Checkbox(
                          value: this._checkBoxFlag,
                          onChanged: (v){
                            setState(() {
                              this._checkBoxFlag=v;
                            });
                          }
                      ),
                      Text("记住密码")
                    ],
                  ),
                )
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(20, 10, 20, 0),
              child: RaisedButton(
                color: Colors.blue,
                child: Container(
                  alignment: Alignment.center,
                  child: Text('登  录',
                    style: TextStyle(fontSize: 20,color: Colors.white),
                  ),
                  height: 60,
                ),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10),
                ),
                onPressed: (){
                  _getText();
                  Future<String> uuPass=get();
                  uuPass.then((String userName) {
                    print(userName);
                    truePassWord=userName;
                  });
                  if(truePassWord == null){
                    Toast.show("账号不存在", context);
                  }else{
                    if(inputPassWord == truePassWord){
                      Toast.show("登陆成功", context);
                      Navigator.push(
                          context,
                          MaterialPageRoute(builder: (BuildContext context) =>
                              LoginSuccessPage2(title: inputUserName,)));
                    }else if (inputPassWord != truePassWord){
                      Toast.show("密码错误", context);
                    }
                  }
                },
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
              child: Container(
                alignment: Alignment.centerRight,
                child: InkWell(
                    child: Text("没有账号？立即注册",
                      style: TextStyle(color: Colors.blue,decoration: TextDecoration.underline),
                    ),
                    onTap: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(builder: (BuildContext context) =>
                              RegisterPage2()));
                    }
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
